import {
    api,
    storage
} from '../util'
import {
    CollectData
} from '../modules/dtcollect';
import authorize from '../modules/authorize.js'
import {
    profile
} from '../modules/store.js'

let this_timer_id = null;
let collectObj = null;
const getUUId = () => {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
    var uuid = s.join("");
    return uuid;
}

const BasePage = (options = {}) => {
    let show = options.onShow;
    let hide = options.onHide;
    let unload = options.onUnload;
    let load = options.onLoad;
    let scroll = options.onPageScroll;
    let defaultOptions = {
        role: "store",
        validateLogin: true,
        isWireShark: true
    }
    options = Object.assign(defaultOptions, options);

    //页面展现
    options.onShow = function () {
        const app = getApp()
        if (this.setData) {
            this.setData({
                fontSizeScale: app.globalData.fontSizeScale
            })
        }
        if (this.scene) {
            console.log(this.scene);
            storage.put("scene", this.scene);
        }
        wx.setNavigationBarColor({
            backgroundColor: api.titleColor,
            frontColor: '#ffffff',
        });
        if (this.validateLogin && !authorize.isLogin(options.role)) {
            if (options.role == "store") {
                profile.loginOut();
            }
            authorize.redirect(options.role);
            return;
        }

        if (show && typeof (show) == "function") {
            show.apply(this, []);
        }
        if (options.isWireShark) {
            onEnter.apply(this, []);
        }

    }
    //页面隐藏
    options.onHide = function () {
        if (hide && typeof (hide) == "function") {
            hide.apply(this, []);
        }
        if (options.isWireShark) {
            onLeave.apply(this, []);
        }
    }
    options.onLoad = function (o) {
        if (load && typeof (load) == "function") {
            load.apply(this, [o]);
        }
        if (o && o.scene) {
            storage.put("scene", o.scene);
        }
        const app = getApp()
        if (this.setData) {
            this.setData({
                fontSizeScale: app.globalData.fontSizeScale
            })
        }
    }

    //滚动
    options.onPageScroll = function (o) {
        if (scroll && typeof (scroll) == "function") {
            scroll.apply(this, [o]);
        }
        if (options.isWireShark) {
            onEnter.apply(this, [o]);
        }


    }
    //页面销毁
    options.onUnload = function () {
        if (unload && typeof (unload) == "function") {
            unload.apply(this, []);
        }
        if (options.isWireShark) {
            onLeave.apply(this, []);
        }


    }

    function onEnter() {
        var that = this;
        if (typeof this_timer_id != null) {
            clearTimeout(this_timer_id);
        }
        this_timer_id = setTimeout(function () {
            collectObj = new CollectData({
                uuid: getUUId(),
                pageKey: that.pageKey
            });
            collectObj.onEnter(options.pageInfo || {})
        }, 200);
    }

    function onLeave() {
        if (collectObj) {
            collectObj.onLeave(this.pageInfo || {});
            collectObj = null;
        }
        if (this_timer_id != null) {
            clearTimeout(this_timer_id);
        }
    }
    return Page(options);
};
export default BasePage